from datetime import datetime
from django.db import models
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from clapton.apps.photos.models import Photo
from tagging.models import Tag
from tagging.fields import TagField
import clapton.utils.markdown as markdown
import clapton.utils.publisher as publisher

class Section(models.Model):
    site = models.ForeignKey(Site, verbose_name=_(u'Site'),) 
    name = models.CharField(_(u'Name'), max_length=250,)
    slug = models.SlugField(_(u'Slug'), primary_key='True', unique=True,
                            help_text=_(u'Automatically built from the section name.'),)
    description = models.TextField(_(u'Description'), blank=True, null=True,
	                           help_text=_(u'Use <a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> syntax.'),)

    class Meta:
        verbose_name_plural = _(u'sections')

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
           return '/stories/%s/' % self.slug

class Story(models.Model):
    user = models.ForeignKey(User, verbose_name=_(u'User'),)
    headline = models.CharField(_(u'Headline'), max_length=250,)
    slug = models.SlugField(_(u'Slug'), unique=True,
                            help_text=_(u'Automatically built from the headline.'),
    )
    assoc_section = models.ForeignKey(Section, verbose_name=_(u'Section'), 
	                              help_text=_(u'Section for the story to belong to.'),)
    body = models.TextField(_(u'Body'), help_text=_(u'Use <a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> syntax.'),)
    tags = TagField(verbose_name=_(u'Tags'), blank=True, null=True, 
	            help_text=_(u'Separate tags with spaces or commas.'))
    pub_date = models.DateTimeField(_(u'Date published'), 
	                            default=datetime.now, 
				    help_text=_(u'Publication date of the entry.'),)
    lead_image = models.ForeignKey(Photo, verbose_name=_(u'Lead image'), 
	                           blank=True, null=True, 
	                           help_text=_(u'Optional lead image for the story.'),)
    enable_comments = models.BooleanField(_(u'Comments enabled'), default=True, 
	                                  help_text=_(u'If checked, comments will be allowed for this story.'),)
    publication_status = models.CharField(_(u'Publication status'), max_length=32, 
	                                  choices=publisher.PUBLICATION_CHOICES,
                                      default='Published',
  					  help_text=_(u'Only published entries will appear on the site. Drafts will only be visible on the Administration page.'),)
    
    objects = models.Manager()
    published_objects = publisher.PublishedManager()

    class Meta:
        verbose_name_plural = _(u'stories')
        ordering = ['-pub_date']
        get_latest_by = 'pub_date'

    def __unicode__(self):
	return self.headline

    def get_absolute_url(self):
        return '/stories/%s/%s/%s/' % (self.assoc_section.slug, self.pub_date.strftime("%Y/%b/%d").lower(),
		                    self.slug)
